package hot_100;

public class 二叉树的最近公共祖先 {
    public static void main(String[] args) {

    }


    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {

        if (root == null || root == p || root == q)
            return root;

        TreeNode lc = lowestCommonAncestor(root.left, p, q);
        TreeNode rc = lowestCommonAncestor(root.right, p, q);
        if (lc != null && rc != null) {
            return root;
        }
        if (lc != null)
            return lc;
        return rc;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

}
